<template src="./template.html"></template>
<style src="./style.css" scoped></style>
<script>
    import api from "api/api";
    import modal from "utils/modal";
    import navigator from "utils/navigator";
    import notification from "utils/notification";
    import constant from "constants/constant";

    export default{
        data(){
            return{
                goods: {},
                itemList: [],
                isShow: false,
                applyType: "退货",
                logisticName: "",
                logisticNumber: "",
                logisticTxt: "",
                errorTxt: null
            }
        },
        created(){
            let self = this;
            let config = weex.config;
            self.goods = config.goods ? config.goods : {};

            //获取物流公司
            api.sendHttp("getLogisticCpApi").then(res=>{
                self.itemList = res.data || [];
            },res=>{
                modal.toast(res.msg || '获取物流公司信息失败');
            });
        },
        methods: {
            showLogisticClick(){
                this.isShow = true;
            },
            choiceClick(e){
                this.logisticName = e;
                this.isShow = false;
            },
            applyReturnGoods(){
                let self = this;
                let params = {
                    type: self.applyType,
                    rid: self.goods.rid,
                    logistics_cp: self.logisticName,
                    logistics_id: self.logisticNumber,
                    logistics_text: self.logisticTxt
                };
                if(self.checkForm()){
                    api.sendHttp("refundUpdateApi",params).then(res=>{
                        modal.toast("申请退货成功");
                        notification.send(constant.EVENT_ORDER_RETURN_GOODS,res.data);
                        navigator.pop(1);
                    },
                    res=>{
                        modal.toast(res.msg || "申请退货失败");
                    });
                }
            },
            checkForm(){
                let self = this;
                //验证用户填写信息
                if(!self.logisticName){
                    self.errorTip("请输入物流公司");
                    return false;
                }

                if(!self.logisticNumber){
                    self.errorTip("请输入物流单号");
                    return false;
                }

                if(self.logisticTxt && self.logisticTxt.length && self.logisticTxt.length >200){
                    self.errorTip("退货说明不能超过200字");
                    return false;
                }

                return true;
            },
            errorTip(tip){
                let self = this;
                self.errorTxt = tip;

                if(self.errorTxt) {
                    setTimeout(function(){
                        self.errorTxt = "";
                    },1500);
                }
            }
        }
    }
</script>
